﻿using Grasshopper;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System.Windows.Forms;

namespace D002_Priority
{
    public class Priority : GH_AssemblyPriority
    {
        public override GH_LoadingInstruction PriorityLoad()
        {
            // 给 Category 设置图标
            Instances.ComponentServer.AddCategoryIcon("PluginTest", Properties.Resources.PluginIcon);

            // 在 CanvasCreated 事件中向 CanvasToolbar 添加一个按钮
            Instances.CanvasCreated += Instances_CanvasCreated;
            return GH_LoadingInstruction.Proceed; // 继续加载插件
        }

        private void Instances_CanvasCreated(GH_Canvas canvas)
        {
            Instances.CanvasCreated -= Instances_CanvasCreated;

            var testButton = new ToolStripButton();

            testButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
            testButton.Image = Properties.Resources.PluginIcon;
            testButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            testButton.Name = "TestButton";
            testButton.Size = new System.Drawing.Size(46, 34);
            testButton.Text = "Test";
            testButton.ToolTipText = "向CanvasToolbar添加的按钮";
            testButton.CheckState = CheckState.Unchecked;

            testButton.Click += TestButton_Click;

            GH_Toolstrip canvasToolbar = (GH_Toolstrip)Instances.DocumentEditor.Controls[0].Controls[1];
            canvasToolbar.Items.Add(testButton);
        }

        private void TestButton_Click(object sender, System.EventArgs e)
        {
            var button = sender as ToolStripButton;
            if (button != null)
            {
                if (button.Checked)
                {
                    button.CheckState = CheckState.Unchecked;
                }
                button.CheckState = CheckState.Checked;

            }
            MessageBox.Show("测试");
        }
    }
}
